Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reshape and document the dht-cache #36

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open

Conversation

lu-zero
Copy link

@lu-zero lu-zero commented Jul 10, 2023

  • Reduce the public API
  • Document it

@codecov
Copy link

codecov bot commented Jul 20, 2023

Codecov Report

Merging #36 (c4000eb) into master (284e00e) will increase coverage by 8.30%.
Report is 7 commits behind head on master.
The diff coverage is 82.17%.

❗ Current head c4000eb differs from pull request most recent head ff3cc90. Consider uploading reports for the commit ff3cc90 to get more accurate results

@@            Coverage Diff             @@
##           master      #36      +/-   ##
==========================================
+ Coverage   71.72%   80.02%   +8.30%     
==========================================
  Files          12       17       +5     
  Lines        1963     2959     +996     
==========================================
+ Hits         1408     2368     +960     
- Misses        555      591      +36     
Files Coverage Δ
dht-cache/src/domopersistentstorage.rs 100.00% <100.00%> (+26.72%) ⬆️
dht-cache/src/utils.rs 100.00% <ø> (ø)
dht-cache/src/data.rs 94.11% <94.11%> (ø)
dht-cache/src/lib.rs 0.00% <0.00%> (ø)
src/domobroker.rs 93.53% <60.00%> (-0.25%) ⬇️
dht-config/src/lib.rs 8.62% <0.00%> (+8.62%) ⬆️
src/main.rs 0.00% <0.00%> (-1.08%) ⬇️
dht-cache/src/domolibp2p.rs 93.33% <90.00%> (+6.54%) ⬆️
dht-cache/src/cache/local.rs 94.97% <94.97%> (ø)
dht-cache/src/domocache.rs 68.85% <41.17%> (+23.86%) ⬆️
... and 2 more

... and 2 files with indirect coverage changes

dht-cache/src/cache.rs Outdated Show resolved Hide resolved
dht-cache/src/cache.rs Outdated Show resolved Hide resolved
dht-cache/src/dht.rs Outdated Show resolved Hide resolved
let mut cache = self.0.write().await;

if let Some(storage) = cache.store.as_mut() {
storage.store(&elem).await;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶

error: this expression creates a reference which is immediately dereferenced by the compiler
  --> dht-cache/src/cache/local.rs:87:27
   |
87 |             storage.store(&elem).await;
   |                           ^^^^^ help: change this to: `elem`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `-D clippy::needless-borrow` implied by `-D warnings`

dht-cache/src/cache/local.rs Outdated Show resolved Hide resolved
dht-cache/src/cache.rs Outdated Show resolved Hide resolved
@lu-zero lu-zero force-pushed the dht-cache-reshape branch 2 times, most recently from 185cc26 to 6a3b9fc Compare August 11, 2023 14:11
dht-cache/src/cache.rs Outdated Show resolved Hide resolved
@lu-zero lu-zero force-pushed the dht-cache-reshape branch 5 times, most recently from 8898e9e to 40306f0 Compare August 18, 2023 11:27
@lu-zero lu-zero force-pushed the dht-cache-reshape branch 2 times, most recently from 0132c71 to 1837b6c Compare August 18, 2023 13:28
Copy link

@dodomorandi dodomorandi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to finish reviewing, I have two major concerns about use of unsafe and select! other things are minor.


for (topic_uuid, value) in map_topic_name.iter() {
topic_uuid.hash(state);
value.to_string().hash(state);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty bad, because it requires N^2 allocations. Considering that a hash fn should be fast, we have a potential problem, even from a cryptographic standpoint.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could call hash over the larger map, I moved the code around but I did not check if the hash value would change.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to easily split these comments into a separated issue? Obviously, for me it is easy to just check the diff, but I agree that the aim here is a bit different. If we can easily split issues that should be fixed in different PRs, it would be great.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly there isn't, it would be a nice feature though.

I'd do a round of performance improvements after we are good with the API rework :)

let topic_name = elem.topic_name.clone();
let topic_uuid = &elem.topic_uuid;

let topic = cache.mem.entry(topic_name).or_default();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could consider using the contains_key + eventual allocation and insertion, instead of using the entry API. Last time I checked this approach for a HashMap (and it was convenient to approach the problem in this way), but I never did any benchmark

dht-cache/src/cache/local.rs Outdated Show resolved Hide resolved
Comment on lines 160 to 162
pub fn new(topic: &str, cache: LocalCache) -> Self {
Self {
topic: topic.to_owned(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn new(topic: &str, cache: LocalCache) -> Self {
Self {
topic: topic.to_owned(),
pub fn new(topic: impl Into<String>, cache: LocalCache) -> Self {
Self {
topic: topic.into(),

Comment on lines 168 to 169
pub fn with_uuid(mut self, uuid: &str) -> Self {
self.uuid = Some(uuid.to_owned());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn with_uuid(mut self, uuid: &str) -> Self {
self.uuid = Some(uuid.to_owned());
pub fn with_uuid(mut self, uuid: impl Into<String>) -> Self {
self.uuid = Some(uuid.into());

dht-cache/src/cache.rs Outdated Show resolved Hide resolved
dht-cache/src/cache.rs Show resolved Hide resolved
dht-cache/src/cache.rs Show resolved Hide resolved
dht-cache/src/cache.rs Outdated Show resolved Hide resolved
dht-cache/src/cache.rs Outdated Show resolved Hide resolved
Copy link

@dodomorandi dodomorandi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just a major concern, most of the things are minor or considerations.

dht-cache/src/cache/local.rs Outdated Show resolved Hide resolved
dht-cache/src/cache/local.rs Show resolved Hide resolved
dht-cache/src/dht.rs Outdated Show resolved Hide resolved
dht-cache/src/dht.rs Outdated Show resolved Hide resolved
return swarm
}
}
ev = swarm.select_next_some() => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be cancel-safe, therefore the select! should be fine. However, this characteristics does not seem to be documented anywhere, therefore I don't think we can assume that future non-breaking versions of libp2p will have this specific behavior. On the other hand, given that the implementation of Stream currently forwards to the internal sync poll_next_event function, it is very plausible that swarm.select_next_some() will continue to be cancel-safe.

If you want to be sure, the resulting future could be saved and pinned in order to keep it across different iterations, but it is noisy and probably it is not worth at this point. What do you think?

dht-cache/src/dht.rs Outdated Show resolved Hide resolved
dht-cache/src/dht.rs Outdated Show resolved Hide resolved
dht-cache/src/domocache.rs Outdated Show resolved Hide resolved
Comment on lines 195 to 199
let peers: Vec<_> = check_peers(&mut swarm);
if !peers.is_empty() &&
ev_send.send(Event::Ready(peers)).is_err() {
return swarm;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting:

Suggested change
let peers: Vec<_> = check_peers(&mut swarm);
if !peers.is_empty() &&
ev_send.send(Event::Ready(peers)).is_err() {
return swarm;
}
let peers: Vec<_> = check_peers(&mut swarm);
if !peers.is_empty() && ev_send.send(Event::Ready(peers)).is_err() {
return swarm;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants